<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <meta name="description" content="上穷碧落下凡尘:Win10系统下基于Docker配置Elasticsearch7配合Python3进行全文检索交互
​    基于文档式的全文检索引擎大家都不陌生，之前一篇文章：使用Redisearch实现的全文检索功能服务，曾经使用Rediseach来小试牛刀了一把，文中戏谑的称Rediseach" />
  

  
  
  
  
  
  
  <title>win10系统下基于docker配置elasticsearch配合python3进行全文检索 | 南辞的技术博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="上穷碧落下凡尘:Win10系统下基于Docker配置Elasticsearch7配合Python3进行全文检索交互 ​    基于文档式的全文检索引擎大家都不陌生，之前一篇文章：使用Redisearch实现的全文检索功能服务，曾经使用Rediseach来小试牛刀了一把，文中戏谑的称Rediseach已经替代了Elasticsearch，其实不然，Elasticsearch作为老牌的全文检索引擎还并">
<meta property="og:type" content="article">
<meta property="og:title" content="win10系统下基于docker配置elasticsearch配合python3进行全文检索">
<meta property="og:url" content="https://yanlidoushikeke.gitee.io/myhexo/2021/04/23/win10%E7%B3%BB%E7%BB%9F%E4%B8%8B%E5%9F%BA%E4%BA%8Edocker%E9%85%8D%E7%BD%AEelasticsearch%E9%85%8D%E5%90%88python3%E8%BF%9B%E8%A1%8C%E5%85%A8%E6%96%87%E6%A3%80%E7%B4%A2/index.html">
<meta property="og:site_name" content="南辞的技术博客">
<meta property="og:description" content="上穷碧落下凡尘:Win10系统下基于Docker配置Elasticsearch7配合Python3进行全文检索交互 ​    基于文档式的全文检索引擎大家都不陌生，之前一篇文章：使用Redisearch实现的全文检索功能服务，曾经使用Rediseach来小试牛刀了一把，文中戏谑的称Rediseach已经替代了Elasticsearch，其实不然，Elasticsearch作为老牌的全文检索引擎还并">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://v3u.cn/v3u/Public/Uploads/1595300814.png">
<meta property="og:image" content="https://v3u.cn/v3u/Public/js/editor/attached/20200721100710_94604.png">
<meta property="og:image" content="https://v3u.cn/v3u/Public/js/editor/attached/20200721110716_89621.png">
<meta property="og:image" content="https://v3u.cn/v3u/Public/js/editor/attached/20200721110732_55526.png">
<meta property="article:published_time" content="2021-04-23T07:26:07.000Z">
<meta property="article:modified_time" content="2021-04-23T07:27:01.811Z">
<meta property="article:author" content="南辞">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://v3u.cn/v3u/Public/Uploads/1595300814.png">
  
  
    <link rel="icon" href="/tefuir/css/images/favicon.ico">
  
  
<link rel="stylesheet" href="/tefuir/css/style.css">

  

  
  <!-- baidu webmaster push -->
  <script src='//push.zhanzhang.baidu.com/push.js'></script>
<meta name="generator" content="Hexo 5.2.0"><link rel="alternate" href="/tefuir/atom.xml" title="南辞的技术博客" type="application/atom+xml">
</head>
<body class="home blog custom-background custom-font-enabled single-author">
  <div id="page" class="hfeed site">
      <header id="masthead" class="site-header" role="banner">
    <hgroup>
      <h1 class="site-title">
        <a href="/tefuir/" title="南辞的技术博客" rel="home">南辞的技术博客</a>
      </h1>
      
        <h2 class="site-description hitokoto"></h2>
        <script type="text/javascript" src="https://v1.hitokoto.cn/?encode=js"></script>
      
    </hgroup>

    <nav id="site-navigation" class="main-navigation" role="navigation">
            <button class="menu-toggle">菜单</button>
            <a class="assistive-text" href="/#content" title="跳至内容">跳至内容</a><!--TODO-->
            <div class="menu-main-container">
                <ul id="menu-main" class="nav-menu">
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/tefuir/">Home</a></li>
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/tefuir/archives">Archives</a></li>
                
                </ul>
            </div>
    </nav>
</header>

      <div id="main" class="wrapper">
        <div id="primary" class="site-content"><div id="content" role="main"><article id="post-win10系统下基于docker配置elasticsearch配合python3进行全文检索" class="post-win10系统下基于docker配置elasticsearch配合python3进行全文检索 post type-post status-publish format-standard hentry">
    <!---->

      <header class="entry-header">
        
        
  
    <h1 class="entry-title article-title">
      win10系统下基于docker配置elasticsearch配合python3进行全文检索
    </h1>
  

        
        <div class="comments-link">
            
            <a href="javascript:void(0);" data-url="https://yanlidoushikeke.gitee.io/myhexo/2021/04/23/win10%E7%B3%BB%E7%BB%9F%E4%B8%8B%E5%9F%BA%E4%BA%8Edocker%E9%85%8D%E7%BD%AEelasticsearch%E9%85%8D%E5%90%88python3%E8%BF%9B%E8%A1%8C%E5%85%A8%E6%96%87%E6%A3%80%E7%B4%A2/" data-id="cknzsqft6000254u0gf5h2eqt" class="leave-reply bdsharebuttonbox" data-cmd="more">Share</a>
        </div><!-- .comments-link -->
      </header><!-- .entry-header -->

    <div class="entry-content">
      
        <h1 id="上穷碧落下凡尘-Win10系统下基于Docker配置Elasticsearch7配合Python3进行全文检索交互"><a href="#上穷碧落下凡尘-Win10系统下基于Docker配置Elasticsearch7配合Python3进行全文检索交互" class="headerlink" title="上穷碧落下凡尘:Win10系统下基于Docker配置Elasticsearch7配合Python3进行全文检索交互"></a>上穷碧落下凡尘:Win10系统下基于Docker配置Elasticsearch7配合Python3进行全文检索交互</h1><p><img src="https://v3u.cn/v3u/Public/Uploads/1595300814.png" alt="上穷碧落下凡尘:Win10系统下基于Docker配置Elasticsearch7配合Python3进行全文检索交互"></p>
<p>​    基于文档式的全文检索引擎大家都不陌生，之前一篇文章：<a target="_blank" rel="noopener" href="https://v3u.cn/a_id_105">使用Redisearch实现的全文检索功能服务</a>，曾经使用Rediseach来小试牛刀了一把，文中戏谑的称Rediseach已经替代了Elasticsearch，其实不然，Elasticsearch作为老牌的全文检索引擎还并没有退出历史舞台，依旧占据主流市场，桃花依旧笑春风，阿里也在其ecs服务中推出了云端Elasticsearch引擎，所以本次我们在Win10系统中依托Docker来感受一下Elasticsearch的魅力。</p>
<p>​    首先安装Docker，具体流程请参照：<a target="_blank" rel="noopener" href="https://v3u.cn/a_id_149">win10系统下把玩折腾DockerToolBox以及更换国内镜像源(各种神坑)</a>，这里不再赘述。</p>
<p>​    拉取Elasticsearch镜像，这里我们使用7.0以上的版本，该版本从性能和效率上都得到了优化。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker pull elasticsearch:7.2.0</span><br></pre></td></tr></table></figure>

<p>​    随后运行Elasticsearch镜像</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run --name es -p 9200:9200 -p 9300:9300 -e &quot;discovery.type&#x3D;single-node&quot; -d elasticsearch:7.2.0</span><br></pre></td></tr></table></figure>

<p>​    容器别名我们就用缩写es来替代，通过 9200 端口并使用 Elasticsearch 的原生 传输 协议和集群交互。集群中的节点通过端口 9300 彼此通信。如果这个端口没有打开，节点将无法形成一个集群，运行模式先走单节点模式。</p>
<p>​    启动容器成功后，可以访问一下浏览器: <a target="_blank" rel="noopener" href="http://localhost:9200/">http://localhost:9200</a></p>
<p><img src="https://v3u.cn/v3u/Public/js/editor/attached/20200721100710_94604.png" alt="img"></p>
<p>​    OK,没有任何问题，Elasticsearch 采用 YAML 文件对系统进行配置，原理很简单，就像Django的settings或者Flask的Config，只要通知Elasticsearch服务在运行过程中一些你想要的功能，而Elasticsearch会找到elasticsearch.yml，之后按你指定的参数运行服务。</p>
<p>​    此时，我们需要将容器内部Elasticsearch的配置文件拷贝出来，这样以后启动容器就可以按照我们自己指定的配置来修改了。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker cp 容器id:&#x2F;usr&#x2F;share&#x2F;elasticsearch&#x2F;config&#x2F;elasticsearch.yml .&#x2F;elasticsearch.yml</span><br></pre></td></tr></table></figure>

<p>​    老规矩，前面的是容器内地址，后面的是宿主机地址，这里我就拷贝到当前目录下，当然了，你也可以指定绝对路径。</p>
<p>​    打开elasticsearch.yml，可以自己加一些配置，比如允许跨域访问，这样你这台Elasticsearch就可以被别的服务器访问了，这是微服务全文检索系统架构的第一步。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">cluster.name: &quot;docker-cluster&quot;</span><br><span class="line">network.host: 0.0.0.0</span><br><span class="line">http.cors.enabled: true</span><br><span class="line">http.cors.allow-origin: &quot;*&quot;</span><br></pre></td></tr></table></figure>

<p>​    然后停止正在运行的Elasticsearch容器，并且删除它。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker stop 容器id</span><br><span class="line">docker rm $(docker ps -a -q)</span><br></pre></td></tr></table></figure>

<p>​    再次启动Elasticsearch容器，这一次不同的是，我们需要通过-v挂载命令把我们刚刚修改好的elasticsearch.yml挂载到容器内部去，这样容器就根据我们自己修改的配置文件来运行Elasticsearch服务。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run --name es -v &#x2F;es&#x2F;elasticsearch.yml:&#x2F;usr&#x2F;share&#x2F;elasticsearch&#x2F;config&#x2F;elasticsearch.yml -p 9200:9200 -p 9300:9300 -e &quot;discovery.type&#x3D;single-node&quot; -d elasticsearch:7.2.0</span><br></pre></td></tr></table></figure>

<p>​    这里需要注意一点，就是在Win10宿主机里需要单独设置一下共享文件夹，这里我设置的共享文件夹叫做es，如果是Centos或者Mac os就直接写真实物理路径即可。</p>
<p>​    这里再简单介绍一下Win10如何设置共享文件夹用来配合Docker的挂载，打开virtualBox设置，新建一个共享文件夹es</p>
<p><img src="https://v3u.cn/v3u/Public/js/editor/attached/20200721110716_89621.png" alt="img">    随后，重启Docker，输入命令进入默认容器：docker-machine ssh default</p>
<p>​    在容器根目录能够看到刚刚设置的共享文件夹，就说明设置成功了。</p>
<p><img src="https://v3u.cn/v3u/Public/js/editor/attached/20200721110732_55526.png" alt="img"></p>
<p>​    另外还有一个需要注意的点，就是Elasticsearch存储数据也可以通过-v命令挂载出来，如果不对数据进行挂载，当容器被停止或者删除，数据也会不复存在，所以挂载后存储在宿主机会比较好一点，命令是：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run --name es -v &#x2F;es&#x2F;elasticsearch.yml:&#x2F;usr&#x2F;share&#x2F;elasticsearch&#x2F;config&#x2F;elasticsearch.yml -v &#x2F;es&#x2F;data:&#x2F;usr&#x2F;share&#x2F;elasticsearch&#x2F;data -p 9200:9200 -p 9300:9300 -e &quot;discovery.type&#x3D;single-node&quot; -d elasticsearch:7.2.0</span><br></pre></td></tr></table></figure>

<p>​    再次启动容器成功之后，我们就可以利用Python3来和全文检索引擎Elasticsearch进行交互了，安装依赖的库。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip3 install elasticsearch</span><br></pre></td></tr></table></figure>

<p>​    新建es_test.py测试脚本</p>
<p>​    建立Elasticsearch的检索实例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">from elasticsearch import Elasticsearch</span><br><span class="line"> </span><br><span class="line">es &#x3D; Elasticsearch(hosts&#x3D;[&#123;&quot;host&quot;:&#39;Docker容器所在的ip&#39;, &quot;port&quot;: 9200&#125;])</span><br></pre></td></tr></table></figure>

<p>​    这里的host指容器ip，因为可以扩展集群，所以是一个list，需要注意一点，如果是Win10就是系统分配的那个ip,Centos或者Mac os直接写127.0.0.1即可。</p>
<p>​    建立索引(Index)，这里我们创建一个名为 article 的索引</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">result &#x3D; es.indices.create(index&#x3D;&#39;article&#39;, ignore&#x3D;400)</span><br><span class="line">print(result)</span><br><span class="line"></span><br><span class="line">&#123;&#39;acknowledged&#39;: True, &#39;shards_acknowledged&#39;: True, &#39;index&#39;: &#39;article&#39;&#125;</span><br></pre></td></tr></table></figure>

<p>​    其中的 acknowledged 字段表示创建操作执行成功。</p>
<p>​    删除索引也是类似的，代码如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">result &#x3D; es.indices.delete(index&#x3D;&#39;article&#39;, ignore&#x3D;[400, 404])</span><br><span class="line">print(result)</span><br><span class="line"></span><br><span class="line">&#123;&#39;acknowledged&#39;: True&#125;</span><br></pre></td></tr></table></figure>

<p>​    插入数据，Elasticsearch 就像 MongoDB 一样，在插入数据的时候可以直接插入结构化字典数据，插入数据可以调用 index() 方法，这里索引和数据是强关联的，所以插入时需要指定之前建立好的索引。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">data &#x3D; &#123;&#39;title&#39;: &#39;我在北京学习人工智能&#39;, &#39;url&#39;: &#39;http:&#x2F;&#x2F;123.com&#39;,&#39;content&#39;:&quot;在北京学习&quot;&#125;</span><br><span class="line">result &#x3D; es.index(index&#x3D;&#39;article&#39;,body&#x3D;data)</span><br><span class="line">print(result)</span><br><span class="line"></span><br><span class="line">&#123;&#39;_index&#39;: &#39;article&#39;, &#39;_type&#39;: &#39;_doc&#39;, &#39;_id&#39;: &#39;GyJgb3MBuQaE6wYOApTh&#39;, &#39;_version&#39;: 1, &#39;result&#39;: &#39;created&#39;, &#39;_shards&#39;: &#123;&#39;total&#39;: 2, &#39;successful&#39;: 1, &#39;failed&#39;: 0&#125;, &#39;_seq_no&#39;: 5, &#39;_primary_term&#39;: 1&#125;</span><br></pre></td></tr></table></figure>

<p>​    可以看到index()方法会自动生成一个唯一id，当然我们也可以使用create()方法创建数据，不同的是create()需要手动指定一个id。</p>
<p>​    修改数据也非常简单，我们同样需要指定数据的 id 和内容，调用 index() 方法即可，代码如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">data &#x3D; &#123;&#39;content&#39;:&quot;在北京学习python&quot;&#125;</span><br><span class="line"></span><br><span class="line">#修改</span><br><span class="line">result &#x3D; es.index(index&#x3D;&#39;article&#39;,body&#x3D;data, id&#x3D;&#39;GyJgb3MBuQaE6wYOApTh&#39;)</span><br><span class="line"></span><br><span class="line">&#123;&#39;_index&#39;: &#39;article&#39;, &#39;_type&#39;: &#39;_doc&#39;, &#39;_id&#39;: &#39;GyJgb3MBuQaE6wYOApTh&#39;, &#39;_version&#39;: 2, &#39;result&#39;: &#39;updated&#39;, &#39;_shards&#39;: &#123;&#39;total&#39;: 2, &#39;successful&#39;: 1, &#39;failed&#39;: 0&#125;, &#39;_seq_no&#39;: 6, &#39;_primary_term&#39;: 1&#125;</span><br></pre></td></tr></table></figure>

<p>​    删除数据，可以调用 delete() 方法，指定需要删除的数据 id 即可</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">#删除</span><br><span class="line">result &#x3D; es.delete(index&#x3D;&#39;article&#39;,id&#x3D;&#39;GyJgb3MBuQaE6wYOApTh&#39;)</span><br><span class="line">print(result)</span><br><span class="line"></span><br><span class="line">&#123;&#39;_index&#39;: &#39;article&#39;, &#39;_type&#39;: &#39;_doc&#39;, &#39;_id&#39;: &#39;GyJgb3MBuQaE6wYOApTh&#39;, &#39;_version&#39;: 3, &#39;result&#39;: &#39;deleted&#39;, &#39;_shards&#39;: &#123;&#39;total&#39;: 2, &#39;successful&#39;: 1, &#39;failed&#39;: 0&#125;, &#39;_seq_no&#39;: 7, &#39;_primary_term&#39;: 1&#125;</span><br></pre></td></tr></table></figure>

<p>​    查询数据，这里可以简单的查询全量数据：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">#查询</span><br><span class="line">result &#x3D; es.search(index&#x3D;&#39;article&#39;)</span><br><span class="line">print(result)</span><br><span class="line"></span><br><span class="line">&#123;&#39;took&#39;: 1079, &#39;timed_out&#39;: False, &#39;_shards&#39;: &#123;&#39;total&#39;: 1, &#39;successful&#39;: 1, &#39;skipped&#39;: 0, &#39;failed&#39;: 0&#125;, &#39;hits&#39;: &#123;&#39;total&#39;: &#123;&#39;value&#39;: 5, &#39;relation&#39;: &#39;eq&#39;&#125;, &#39;max_score&#39;: 1.0, &#39;hits&#39;: [&#123;&#39;_index&#39;: &#39;article&#39;, &#39;_type&#39;: &#39;blog&#39;, &#39;_id&#39;: &#39;1&#39;, &#39;_score&#39;: 1.0, &#39;_source&#39;: &#123;&#39;title&#39;: &#39;我在北京学习人工智能&#39;, &#39;url&#39;: &#39;http:&#x2F;&#x2F;123.com&#39;, &#39;content&#39;: &#39;在北京学习&#39;&#125;&#125;, &#123;&#39;_index&#39;: &#39;article&#39;, &#39;_type&#39;: &#39;blog&#39;, &#39;_id&#39;: &#39;FyIdb3MBuQaE6wYO8JQR&#39;, &#39;_score&#39;: 1.0, &#39;_source&#39;: &#123;&#39;title&#39;: &#39;你好&#39;, &#39;content&#39;: &#39;你好123&#39;&#125;&#125;, &#123;&#39;_index&#39;: &#39;article&#39;, &#39;_type&#39;: &#39;blog&#39;, &#39;_id&#39;: &#39;GCIeb3MBuQaE6wYOnpSv&#39;, &#39;_score&#39;: 1.0, &#39;_source&#39;: &#123;&#39;title&#39;: &#39;你好&#39;, &#39;url&#39;: &#39;http:&#x2F;&#x2F;123.com&#39;, &#39;content&#39;: &#39;你好123&#39;&#125;&#125;, &#123;&#39;_index&#39;: &#39;article&#39;, &#39;_type&#39;: &#39;blog&#39;, &#39;_id&#39;: &#39;GSJfb3MBuQaE6wYOu5RD&#39;, &#39;_score&#39;: 1.0, &#39;_source&#39;: &#123;&#39;title&#39;: &#39;你好&#39;, &#39;url&#39;: &#39;http:&#x2F;&#x2F;123.com&#39;, &#39;content&#39;: &#39;你好123&#39;&#125;&#125;, &#123;&#39;_index&#39;: &#39;article&#39;, &#39;_type&#39;: &#39;blog&#39;, &#39;_id&#39;: &#39;GiJfb3MBuQaE6wYO5pR4&#39;, &#39;_score&#39;: 1.0, &#39;_source&#39;: &#123;&#39;title&#39;: &#39;你好&#39;, &#39;url&#39;: &#39;http:&#x2F;&#x2F;123.com&#39;, &#39;content&#39;: &#39;你好123&#39;&#125;&#125;]&#125;&#125;</span><br></pre></td></tr></table></figure>

<p>​    还可以进行全文检索，这才是体现 Elasticsearch 搜索引擎特性的地方。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">mapping &#x3D; &#123;</span><br><span class="line">    &#39;query&#39;: &#123;</span><br><span class="line">        &#39;match&#39;: &#123;</span><br><span class="line">            &#39;content&#39;: &#39;学习 北京&#39;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">result &#x3D; es.search(index&#x3D;&#39;article&#39;,body&#x3D;mapping)</span><br><span class="line">print(result)</span><br><span class="line"></span><br><span class="line">&#123;&#39;took&#39;: 4, &#39;timed_out&#39;: False, &#39;_shards&#39;: &#123;&#39;total&#39;: 1, &#39;successful&#39;: 1, &#39;skipped&#39;: 0, &#39;failed&#39;: 0&#125;, &#39;hits&#39;: &#123;&#39;total&#39;: &#123;&#39;value&#39;: 1, &#39;relation&#39;: &#39;eq&#39;&#125;, &#39;max_score&#39;: 4.075481, &#39;hits&#39;: [&#123;&#39;_index&#39;: &#39;article&#39;, &#39;_type&#39;: &#39;blog&#39;, &#39;_id&#39;: &#39;1&#39;, &#39;_score&#39;: 4.075481, &#39;_source&#39;: &#123;&#39;title&#39;: &#39;我在北京学习人工智能&#39;, &#39;url&#39;: &#39;http:&#x2F;&#x2F;123.com&#39;, &#39;content&#39;: &#39;在北京学习&#39;&#125;&#125;]&#125;&#125;</span><br></pre></td></tr></table></figure>

<p>​    可以看出，检索时会对对应的字段全文检索，结果还会按照检索关键词的相关性进行排序，这就是一个基本的搜索引擎雏形。</p>
<p>​    除了这些最基本的操作，Elasticsearch还支持很多复杂的查询，可以参照最新的7.2版本文档：<a target="_blank" rel="noopener" href="https://www.elastic.co/guide/en/elasticsearch/reference/7.2/query-dsl.html">https://www.elastic.co/guide/en/elasticsearch/reference/7.2/query-dsl.html</a></p>
<p>​    结语：体验了之后，有人说，Elasticsearch这玩意还真不错，能不能把Mysql或者Mongo全都扔了，就拿它当数据库不就完事了吗？答案当然是不可能的，因为Elasticsearch没有事务，而且是查询是近实时，写入速度很慢，只是读取数据快，成本也比数据库高，几乎就在靠吃内存提高性能，它目前只是作为搜索引擎的存在，如果你的业务涉及全文检索，那么它就是你的首选方案之一。</p>

      
    </div><!-- .entry-content -->

    <footer class="entry-meta">
    <a href="/tefuir/2021/04/23/win10%E7%B3%BB%E7%BB%9F%E4%B8%8B%E5%9F%BA%E4%BA%8Edocker%E9%85%8D%E7%BD%AEelasticsearch%E9%85%8D%E5%90%88python3%E8%BF%9B%E8%A1%8C%E5%85%A8%E6%96%87%E6%A3%80%E7%B4%A2/">
    <time datetime="2021-04-23T07:26:07.000Z" class="entry-date">
        2021-04-23
    </time>
</a>
    
    
    </footer>
</article>


    
<nav class="nav-single">
    <h3 class="assistive-text">文章导航</h3>
    
        <span class="nav-previous"><a href="/tefuir/2021/04/23/win10%E7%B3%BB%E7%BB%9F%E4%B8%8B%E5%88%A9%E7%94%A8docker%E9%83%A8%E7%BD%B2gunicorn-Flask%E6%89%93%E9%80%A0%E7%8B%AC%E7%AB%8B%E9%95%9C%E5%83%8F/" rel="prev"><span class="meta-nav">←</span> win10系统下利用docker部署gunicorn+Flask打造独立镜像</a></span>
    
    
        <span class="nav-next"><a href="/tefuir/2021/04/23/%E6%9C%8D%E5%8A%A1%E5%99%A8%E9%97%AE%E9%A2%98/" rel="next">服务器问题 <span class="meta-nav">→</span></a></span>
    
</nav><!-- .nav-single -->







</div></div>
        <div id="secondary" class="widget-area" role="complementary">
  
    <aside id="search" class="widget widget_search"><form role="search" method="get" accept-charset="utf-8" id="searchform" class="searchform" action="//google.com/search">
    <div>
        <input type="text" value="" name="s" id="s" />
        <input type="submit" id="searchsubmit" value="搜索" />
    </div>
</form></aside>
  
    
  
    
  
    
  <aside class="widget">
    <h3 class="widget-title">Recents</h3>
    <div class="widget-content">
      <ul>
        
          <li>
            <a href="/tefuir/2021/04/26/python%E5%9F%BA%E7%A1%80%E7%90%86%E8%AE%BA%E5%AE%9D%E5%85%B8/">python基础理论宝典</a>
          </li>
        
          <li>
            <a href="/tefuir/2021/04/23/python3-Flask%E7%BB%93%E5%90%88Socket-io%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0%E5%9C%A8%E7%BA%BF%E5%AE%A2%E6%9C%8D%E7%B3%BB%E7%BB%9F/">python3+Flask结合Socket.io简单实现在线客服系统</a>
          </li>
        
          <li>
            <a href="/tefuir/2021/04/23/win10%E7%B3%BB%E7%BB%9F%E4%B8%8B%E5%88%A9%E7%94%A8docker%E9%83%A8%E7%BD%B2gunicorn-Flask%E6%89%93%E9%80%A0%E7%8B%AC%E7%AB%8B%E9%95%9C%E5%83%8F/">win10系统下利用docker部署gunicorn+Flask打造独立镜像</a>
          </li>
        
          <li>
            <a href="/tefuir/2021/04/23/win10%E7%B3%BB%E7%BB%9F%E4%B8%8B%E5%9F%BA%E4%BA%8Edocker%E9%85%8D%E7%BD%AEelasticsearch%E9%85%8D%E5%90%88python3%E8%BF%9B%E8%A1%8C%E5%85%A8%E6%96%87%E6%A3%80%E7%B4%A2/">win10系统下基于docker配置elasticsearch配合python3进行全文检索</a>
          </li>
        
          <li>
            <a href="/tefuir/2021/04/23/%E6%9C%8D%E5%8A%A1%E5%99%A8%E9%97%AE%E9%A2%98/">服务器问题</a>
          </li>
        
      </ul>
    </div>
  </aside>

  
    
  
    
  
</div>
      </div>
      <footer id="colophon" role="contentinfo">
    <p>&copy; 2021 南辞
    All rights reserved.</p>
    <p>Powered by <a href="http://hexo.io/" target="_blank">Hexo</a></p>
</footer>
    <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"1","bdMiniList":false,"bdPic":"","bdStyle":"2","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='/js/share.js'];</script>

<script src="/js/jquery-3.3.1.min.js"></script>


  
<link rel="stylesheet" href="/tefuir/fancybox/jquery.fancybox.css">

  
<script src="/tefuir/fancybox/jquery.fancybox.pack.js"></script>




<script src="/tefuir/js/script.js"></script>


<script src="/js/navigation.js"></script>

<div id="bg"></div>

  </div>
</body>
</html>